// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <string>
#include <vector>

#include <pollux/expression/function_metadata.h>
#include <pollux/expression/function_signature.h>
#include <pollux/type/type.h>

namespace kumo::pollux {

    using FunctionSignatureMap = std::
    unordered_map<std::string, std::vector<const exec::FunctionSignature *> >;

    /// Returns a mapping of all Simple and Vector functions registered in Pollux
    /// The mapping is function name -> list of function signatures
    FunctionSignatureMap get_function_signatures();

    /// Returns a list of function signatures for a given function name. Returns
    /// empty list if function with specified name not found.
    std::vector<const exec::FunctionSignature *> get_function_signatures(
        const std::string &functionName);

    /// Returns a mapping of all Vector functions registered in Pollux
    /// The mapping is function name -> list of function signatures
    FunctionSignatureMap getVectorFunctionSignatures();

    /// Returns if a function is deterministic by fetching all registry entries for
    /// the given function name and checking if all of them are deterministic.
    /// Returns std::nullopt if the function is not found. Returns false if any of
    /// the entries are not deterministic.
    std::optional<bool> isDeterministic(const std::string &functionName);

    /// Given a function name and argument types, returns
    /// the return type if function exists otherwise returns nullptr
    TypePtr resolve_function(
        const std::string &functionName,
        const std::vector<TypePtr> &argTypes);

    /// Given a function name and argument types, returns a pair of return
    /// type and metadata if function exists. Otherwise, returns std::nullopt.
    std::optional<std::pair<TypePtr, exec::VectorFunctionMetadata> >
    resolveFunctionWithMetadata(
        const std::string &functionName,
        const std::vector<TypePtr> &argTypes);

    /// Given a function name and argument types, returns the return type if the
    /// function exists or is a special form that supports type resolution (see
    /// resolveCallableSpecialForm), otherwise returns nullptr.
    TypePtr resolveFunctionOrCallableSpecialForm(
        const std::string &functionName,
        const std::vector<TypePtr> &argTypes);

    /// Given the name of a special form and argument types, returns
    /// the return type if the special form exists and is supported, otherwise
    /// returns nullptr.
    /// Special forms are not supported by this function if:
    /// 1) they cannot be invoked as a CallExpr, e.g. FieldReference.
    /// or
    /// 2) their return types cannot be inferred from their argument types, e.g.
    ///    Cast.
    TypePtr resolveCallableSpecialForm(
        const std::string &functionName,
        const std::vector<TypePtr> &argTypes);

    /// Given name of simple function and argument types, returns
    /// the return type if function exists otherwise returns nullptr
    TypePtr resolveSimpleFunction(
        const std::string &functionName,
        const std::vector<TypePtr> &argTypes);

    /// Given name of vector function and argument types, returns
    /// the return type if function exists otherwise returns nullptr
    TypePtr resolveVectorFunction(
        const std::string &functionName,
        const std::vector<TypePtr> &argTypes);

    /// Given name of a vector function and argument types, returns a pair of return
    /// type and metadata if function exists. Otherwise, returns std::nullopt.
    std::optional<std::pair<TypePtr, exec::VectorFunctionMetadata> >
    resolveVectorFunctionWithMetadata(
        const std::string &functionName,
        const std::vector<TypePtr> &argTypes);

    /// Given name of a function, removes it from both the simple and vector
    /// function registries (including all signatures).
    void removeFunction(const std::string &functionName);

    /// Clears the function registry.
    void clearFunctionRegistry();
} // namespace kumo::pollux
